#include "gfx/legato/generated/screen/le_gen_screen_Screen0.h"
#include <p32xxxx.h>    //ys added          //レジスタ名など引き当ての為    //LATDbits.LATD0 = 1; 

#include "definitions.h"    //必須 ys added //これがないと　SYS_TIME_CallbackRegisterMS()がコンパイエラー

// screen member widget declarations
leWidget* root0;

leWidget* Screen0_BackgroundPanel;
leButtonWidget* Screen0_LedBtn;

static leBool initialized = LE_FALSE;
static leBool showing = LE_FALSE;

int Led_Mode;
int ChangeNG;

leResult screenInit_Screen0(void)
{
    if(initialized == LE_TRUE)
        return LE_FAILURE;

    initialized = LE_TRUE;

    return LE_SUCCESS;
}

leResult screenShow_Screen0(void)
{
    if(showing == LE_TRUE)
        return LE_FAILURE;

    // layer 0
    root0 = leWidget_New();
    root0->fn->setSize(root0, 320, 240);
    root0->fn->setBackgroundType(root0, LE_WIDGET_BACKGROUND_NONE);
    root0->fn->setMargins(root0, 0, 0, 0, 0);
    root0->flags |= LE_WIDGET_IGNOREEVENTS;
    root0->flags |= LE_WIDGET_IGNOREPICK;

    Screen0_BackgroundPanel = leWidget_New();
    Screen0_BackgroundPanel->fn->setPosition(Screen0_BackgroundPanel, 1, 0);
    Screen0_BackgroundPanel->fn->setSize(Screen0_BackgroundPanel, 320, 240);
    Screen0_BackgroundPanel->fn->setScheme(Screen0_BackgroundPanel, &BaseGreen);
    root0->fn->addChild(root0, (leWidget*)Screen0_BackgroundPanel);

    Screen0_LedBtn = leButtonWidget_New();
    Screen0_LedBtn->fn->setPosition(Screen0_LedBtn, 65, 90);
    Screen0_LedBtn->fn->setSize(Screen0_LedBtn, 190, 70);
    Screen0_LedBtn->fn->setScheme(Screen0_LedBtn, &BaseSkyBlue_TextBlack);
    Screen0_LedBtn->fn->setString(Screen0_LedBtn, (leString*)&string_LED_On_Off);
    Screen0_LedBtn->fn->setPressedEventCallback(Screen0_LedBtn, event_Screen0_LedBtn_OnPressed);
    root0->fn->addChild(root0, (leWidget*)Screen0_LedBtn);

    leAddRootWidget(root0, 0);
    leSetLayerColorMode(0, LE_COLOR_MODE_RGB_565);

    showing = LE_TRUE;

    return LE_SUCCESS;
}

void LedFunc(void)
{
        if(Led_Mode == 0)
    {
        Led_Mode = 1;
        LATGbits.LATG15 = 1;     //LED1点灯      //RG15(1)
        LATDbits.LATD4 = 1;    //LED2      //RD4(81)
        LATAbits.LATA15 = 1;    //LED3      //RA15(67)
        LATAbits.LATA14 = 1;    //LED4      //RA(66)
        
    }
    else
    {
        Led_Mode = 0;
        LATGbits.LATG15 = 0;     //LED1消灯
        LATDbits.LATD4 = 0;    //LED2
        LATAbits.LATA15 = 0;    //LED3
        LATAbits.LATA14 = 0;    //LED4
    }
    
}

//ワンショットタイマ　コールバック関数
void TimerCallback_LedBtn ( uintptr_t context )    //ボタン誤動作防止、500msecワンショットタイマ 
{
    ChangeNG = 0;   //500msec経過したので、LED On/Off制御許可
}



void event_Screen0_LedBtn_OnPressed(leButtonWidget* btn)
{
    if(ChangeNG == 0)   //LED On/Off制御 許可の場合  //チャタリング防止
    {
        LedFunc();  //LED： ON/OFF
        ChangeNG = 1;   //LED on/Off制御 不許可
        SYS_TIME_CallbackRegisterMS(TimerCallback_LedBtn, 0, 500, SYS_TIME_SINGLE );    //500msec遅延
    }

    
}

void screenUpdate_Screen0(void)
{
}

void screenHide_Screen0(void)
{

    leRemoveRootWidget(root0, 0);
    leWidget_Delete(root0);
    root0 = NULL;

    Screen0_BackgroundPanel = NULL;
    Screen0_LedBtn = NULL;


    showing = LE_FALSE;
}

void screenDestroy_Screen0(void)
{
    if(initialized == LE_FALSE)
        return;

    initialized = LE_FALSE;
}

leWidget* screenGetRoot_Screen0(uint32_t lyrIdx)
{
    if(lyrIdx >= LE_LAYER_COUNT)
        return NULL;

    switch(lyrIdx)
    {
        case 0:
        {
            return root0;
        }
        default:
        {
            return NULL;
        }
    }
}

